package day03.acc

import org.apache.spark.{SparkConf, SparkContext}

/**
 * @Author wsl
 * @Description  闭包检查，有闭包就需要序列化
 */
object Serializable02 {
  def main(args: Array[String]): Unit = {
    val conf: SparkConf = new SparkConf().setAppName("s2").setMaster("local[*]")
    val sc = new SparkContext(conf)

    val u1 = new User
    u1.name = "zs"

    val u2 = new User
    u2.name = "ls"

    val u3 = new User
    u3.name = "ww"

    sc.makeRDD(List(1, 2, 3, 4))
      .foreach {
        num => println("age=" + (u1.age + num))
      }

    //跨进程通信,算子在executor端用到了在driver端的User对象，所以需要序列化
    sc.makeRDD(List(u1,u2))
      .foreach(
        user=>println(user.name)
      )

    //闭包检查 用到了外部变量u3
    sc.makeRDD(List(u1,u2))
      .foreach(
        user=>println(user.name+""+u3.name)
      )


//    sc.makeRDD(List())
//      .foreach(
//        user=>println(user)
//      )

    sc.stop()
  }

}

//序列化方式 否则：Caused by: java.io.NotSerializableException: serial.User
//1.继承
//class User extends  Serializable {
//2.样例类
case class User() {
  var name: String = _
  var age = 30
}